﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;

namespace ChaterFuncLib
{
    public static class Service
    {
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string strClassName, string strWindowName);
        [DllImport("user32.dll")]
        private static extern IntPtr SetForegroundWindow(IntPtr hWnd);

        public static string ServerVersion = "1.0.0.0 beta";

        public static bool isRunning(string winname)
        {
            IntPtr hWnd = FindWindow(null, winname);
            if ((int)hWnd != 0)
            {
                SetForegroundWindow(hWnd);
                return true;
            }
            return false;
        }

        public static bool isClosed()
        {
            bool closed = Config.GetConfig("Network", "Closed") == "1";
            if (closed)
                return true;
            return false;
        }

        public static void SetClosed(bool closetype)
        {
            Config.SetConfig("Network", "Closed", closetype ? "1" : "0");
        }

        public static IPAddress GetClientIP(TcpClient client)
        {
            string addr = client.Client.RemoteEndPoint.ToString();
            string newip = string.Empty;
            foreach (char i in addr)
            {
                if (i != ':')
                    newip += i;
                else
                    break;
            }
            return IPAddress.Parse(newip);
        }

        /// <summary>
        /// Проверка на существование порта
        /// </summary>
        /// <param name="port">Проверяемый порт</param>
        /// <returns></returns>
        public static bool isPort(uint port)
        {
            if (port > IPEndPoint.MaxPort)
                return false;
            return true;
        }
    };
};